package com.dhcc.config;

import com.dhcc.Interceptor.HeaderLoggingInterceptor;
import jakarta.annotation.Resource;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * WebConfig 类用于配置 Spring MVC 的拦截器
 *
 * @ProjectName Dhcc-Cloud
 * @PackageName com.dhcc.config
 * @Title WebConfig
 * @Date 2024/10/31 上午3:36
 * @Author LiuGuoting
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {
    private static final Logger logger = LoggerFactory.getLogger(HeaderLoggingInterceptor.class);
    @Resource
    private HeaderLoggingInterceptor headerLoggingInterceptor;

    /**
     * 从配置文件中读取属性，是否开启内部鉴权
     * 默认值为 false，不开启内部鉴权
     */
    @Value("${dhcc.intercept:false}")
    private boolean interceptEnabled;
    /**
     * 添加拦截器的方法
     *
     * @param registry 拦截器注册中心，用于添加自定义拦截器
     */
    @Override
    public void addInterceptors(@NotNull InterceptorRegistry registry) {
        // 如果开启了内部鉴权，则注册自定义拦截器
        if (interceptEnabled) {
            logger.info("已启用内部请求校验拦截器");
            registry.addInterceptor(headerLoggingInterceptor);
        }else {
            logger.info("未启用内部请求校验拦截器,如需启用，请参阅README.md");
        }
    }
}
